//
//  SubjectAppView.m
//  LimitFree
//
//  Created by lijinghua on 16/3/2.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "SubjectAppView.h"
#import "AppDetailViewController.h"

@implementation SubjectAppView{
    SubjectApplicationModel *_model;
}

//通过xib生成view，会调用initWithCoder
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        self.layer.cornerRadius = 8;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
        //内部控件还没有创建，对内部控件的设置退出
    }
    
    [self addTapGestureRecognizer];
    return self;
}

- (void)addTapGestureRecognizer{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    [self addGestureRecognizer:tap];
}

- (void)handleTapGesture:(UITapGestureRecognizer*)tap{
    //要把当前选中的应用详情推到导航控制中
    NSString *applicationId = _model.applicationId;
    AppDetailViewController *appDetailVC = [[AppDetailViewController alloc]init];
    appDetailVC.applicationId = applicationId;
    
    //找到UIViewController ，使用事件响应链
    UIResponder *object = self.nextResponder;
    while (object != nil && ![object isKindOfClass:[UIViewController class]]) {
        object = object.nextResponder;
    }
    UIViewController *viewController = (UIViewController*)object;
    [viewController.navigationController pushViewController:appDetailVC animated:YES];
}

- (void)updateWith:(SubjectApplicationModel*)model
{
    _model = model;
    self.iconImageView.layer.cornerRadius = 8;
    self.iconImageView.layer.masksToBounds = YES;
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    self.appTitleLabel.text = model.name;
    self.commentLabel.text = model.ratingOverall;
    self.downloadLabel.text = model.downloads;
    [self.starLevel setStarLevel:model.starOverall];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
